package link.jfire.socket.socketserver.transfer.server;

import java.io.File;
import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.core.JfireContext;
import link.jfire.core.JfireContextImpl;
import link.jfire.core.bean.BeanConfig;
import link.jfire.socket.socketserver.handler.impl.AuthHandler;
import link.jfire.socket.socketserver.handler.impl.ReceiveKeyHandler;
import link.jfire.socket.socketserver.interceptor.impl.AESEncryptIntercepror;
import link.jfire.socket.socketserver.interceptor.impl.AuthInterceptor;
import link.jfire.socket.socketserver.util.HeadFactory;

public class ServerMain
{
    protected AioServer    aioServer;
    protected JfireContext jfireContext;
    private Logger         logger = ConsoleLogFactory.getLogger();
    
    public ServerMain(ServerConfig serverConfig, BeanConfig... beanConfigs)
    {
        serverConfig.addPackageNames("link.jfire.socket");
        init(serverConfig, null, beanConfigs);
    };
    
    public ServerMain(ServerConfig serverConfig, File configFile)
    {
        serverConfig.addPackageNames("link.jfire.socket");
        init(serverConfig, configFile);
    }
    
    protected void init(ServerConfig serverConfig, File configFile, BeanConfig... beanConfigs)
    {
        jfireContext = new JfireContextImpl(serverConfig.getPackageNames());
        for (BeanConfig each : beanConfigs)
        {
            jfireContext.addBeanConfig(each);
        }
        if (configFile != null)
        {
            jfireContext.readConfig(configFile);
        }
        jfireContext.addSingletonEntity(ServerConfig.class.getName(), serverConfig);
        jfireContext.addSingletonEntity(HeadFactory.class.getName(), serverConfig.getHeadFactory());
        jfireContext.addSingletonEntity(serverConfig.getWaitStrategy().getClass().getName(), serverConfig.getWaitStrategy());
        BeanConfig beanConfig = new BeanConfig("link.jfire.socket.socketserver.bus.CenterSwitch");
        beanConfig.putParam("size", String.valueOf(serverConfig.getRingArraySize()));
        jfireContext.addBeanConfig(beanConfig);
        if (serverConfig.isActiveAuth())
        {
            jfireContext.addBean(AuthInterceptor.class.getName(), false, AuthInterceptor.class);
            jfireContext.addBean(AESEncryptIntercepror.class.getName(), false, AESEncryptIntercepror.class);
        }
        try
        {
            jfireContext.initContext();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        aioServer = (AioServer) jfireContext.getBean(AioServer.class.getName());
        if (serverConfig.isActiveAuth())
        {
            AuthHandler authHandler = jfireContext.getBean(AuthHandler.class);
            authHandler.setPrivateKey(serverConfig.getPrivateKey());
            ReceiveKeyHandler receiveKeyHandler = jfireContext.getBean(ReceiveKeyHandler.class);
            receiveKeyHandler.setPrivateKey(serverConfig.getPrivateKey());
        }
    }
    
    /**
     * 使用初始化的时候的端口启动服务器
     */
    public void start()
    {
        aioServer.start();
    }
    
    public void stop()
    {
        logger.warn("服务器被指令关闭");
        aioServer.stop();
    }
    
    /**
     * 返回服务器内部使用的beancontext实例
     * 
     * @return
     */
    public JfireContext getContext()
    {
        return jfireContext;
    }
}
